import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import static java.lang.Thread.sleep;
import static java.util.concurrent.TimeUnit.*;

public class Main {
    public static void main(String[] args) throws InterruptedException, IOException {
        //Test01();
        //Test02();
        //Test03();
//        Test04();
//        Test05();
//        Text06();
//        Text07();
//        Text08();
//        Text09();
//        Text10();
//        Test11();
        Test12();

    }

    private static void Test12() throws InterruptedException, IOException {
        WebDriver webDriver = new ChromeDriver();

        webDriver.get("https://www.baidu.com/");

        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();

        String cur_handle = webDriver.getWindowHandle();

        Set<String> all_handles = webDriver.getWindowHandles();
        String temp = "";

        for (String handle : all_handles) {
            System.out.println(handle);
            temp = handle;
        }
        webDriver.switchTo().window(temp);

        webDriver.findElement(By.cssSelector("#header-link-wrapper > li:nth-child(5) > a")).click();

        sleep(3000);

        File file = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file, new File("D://截图2024.04.16"));

        webDriver.close();

        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrolTop=10000");
        sleep(3000);

        webDriver.findElement(By.cssSelector("#kw")).sendKeys("二哈");

    }

    //截图并保存到D盘中（需要从maven仓库中导包）
    private static void Test11() throws IOException, InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("二哈");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        File file = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file, new File("D://掘图2024-4-9.png"));

        webDriver.quit();
    }


    //切换窗口
    private static void Text10() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");

        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(3000);

        String cur_handle = webDriver.getWindowHandle();
        System.out.println("当前窗口句柄等于：" + cur_handle);

        Set<String> all_handles = webDriver.getWindowHandles();
        String target = "";
        for (String temp : all_handles) {
            target = temp;
        }
        webDriver.switchTo().window(target);
        sleep(3000);

        webDriver.findElement(By.cssSelector("#ww")).sendKeys("新闻头条");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();
        sleep(3000);

        webDriver.quit();
    }

    //浏览器滚动条（上划/下滑）
    private static void Text09() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("鲜花");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);

        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
        sleep(3000);
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=0");
        sleep(3000);

        webDriver.quit();
    }

    //测试前进回退操作
    private static void Text08() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("张三");
        webDriver.findElement(By.cssSelector("#su")).click();
        webDriver.manage().timeouts().implicitlyWait(3, SECONDS);

        webDriver.navigate().back();
        webDriver.manage().timeouts().implicitlyWait(3, SECONDS);

        String url = webDriver.getCurrentUrl();
        System.out.println(url);

        if (url.equals("https://www.baidu.com/")) {
            System.out.println("测试成功");
        } else {
            System.out.println("测试失败");
        }
        webDriver.quit();
    }

    //设置窗口大小
    private static void Text07() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");

        webDriver.manage().window().setSize(new Dimension(200, 400));

        sleep(3000);

        webDriver.quit();
    }

    //getCurrentUrl() 获取到当前URL
    //getTitle() 获取到当前标题
    private static void Text06() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.manage().window().maximize();

        System.out.println(webDriver.findElement(By.cssSelector("a span")).getText());
        System.out.println(webDriver.getTitle());
        System.out.println(webDriver.getCurrentUrl());

        webDriver.manage().timeouts().implicitlyWait(3, MILLISECONDS);
        webDriver.quit();
    }

    //getAttribute是获取元素的属性值，其中value代表的是元素的属性，如果元素没有value这个属性，此时结果是null
    private static void Test05() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        WebElement search_input = webDriver.findElement(By.cssSelector("#su"));
        System.out.println(search_input.getAttribute("value"));
    }

    //submit可以使用在form标签中的元素
    //click是都可以用
    private static void Test04() {
        //TrueTest();
        FalseTest();
    }

    private static void FalseTest() {
        //创建一个驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com");
        //定位百度搜索栏
        WebElement search_input = webDriver.findElement(By.cssSelector("#kw"));
        //点击新闻
        WebElement baidu_button = webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)"));
        //点击搜索
        baidu_button.submit();
    }

    private static void TrueTest() {
        //创建一个驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com");
        //定位百度搜索栏
        WebElement search_input = webDriver.findElement(By.cssSelector("#kw"));
        //点击新闻
        WebElement baidu_button = webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)"));
        //点击搜索
        baidu_button.click();
    }

    private static void Test03() throws InterruptedException {
        //创建一个驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com");
        //找到搜索栏
        WebElement search_input = webDriver.findElement(By.cssSelector("#kw"));
        //向搜索栏中输入内容
        search_input.sendKeys("软件测试");
        //找到百度一下的按钮
        WebElement search_button = webDriver.findElement(By.cssSelector("#su"));
        //清空百度搜索栏中的内容
        search_input.clear();
        //向搜索栏中重新输入内容
        search_input.sendKeys("前端");
        //点击搜索
        search_button.click();
        //智能等待
        webDriver.manage().timeouts().implicitlyWait(5, DAYS);
        //强制等待
        //sleep(3000);

        //校验结果是不是有前端结果
        List<WebElement> search_result = webDriver.findElements(By.cssSelector("a em"));
        //遍历结果集
        for (WebElement webElement : search_result) {
            if (webElement.getText().equals("前端")) {
                System.out.println("测试成功");
            } else {
                System.out.println("测试失败");
            }
        }
    }


    /*  鼠标点击与键盘输入
    click 点击对象
    send_keys 在对象上模拟按键输入
    clear 清除对象输入的文本内容
    text 用于获取元素的文本信息
     */
    private static void Test02() throws InterruptedException {
        //创建一个驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com");
        //定位百度搜索栏
        WebElement search_input = webDriver.findElement(By.cssSelector("#kw"));
        //向搜索框中输入“软件测试”
        search_input.sendKeys("软件测试");
        //找到百度一下
        WebElement baidu_button = webDriver.findElement(By.cssSelector("#su"));
        //点击搜索
        baidu_button.click();
        sleep(3000);
        //查询结果中
        List<WebElement> search_results = webDriver.findElements(By.cssSelector("a em"));
        sleep(3000);
        //遍历list
        for (int i = 0; i < search_results.size(); i++) {
            if (search_results.get(i).getText().equals("软件测试")) {
                System.out.println("测试成功");
            } else {
                System.out.println("测试失败");
            }
        }
        webDriver.quit();
    }


    // findElement、get、quit
    private static void Test01() {
        //创建一个驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com");
        //定位百度搜索框（通过css选择器定位元素）
        WebElement search_input = webDriver.findElement(By.cssSelector(".s_ipt"));
        //通过xpath定位元素
        WebElement search_input01 = webDriver.findElement(By.xpath("//form/span[1]/input"));
        //通过标签定位
        WebElement search_input02 = webDriver.findElement(By.tagName("input"));

        if (search_input02 == null) {
            System.out.println("测试不通过");
        } else {
            System.out.println("测试通过");
        }
        //关闭浏览器
        webDriver.quit();
    }
}
